<?php

namespace plugin\admin\app\model;

use Illuminate\Database\Eloquent\Relations\HasMany;
use plugin\admin\app\model\Base;
use const support\CORRECT;

class RlGoods extends Base
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'rl_goods';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    //关联图片组
    public function goodsImages(): HasMany
    {
        return $this->hasMany('app\model\GoodsImagesModel','goods_id','id')->where('status',1)->orderBy('sort','desc');
    }

    public function getAllocationNumAttribute($value)
    {
        if (!$this->is_allocation_mode){
            return '';
        }
        if ($this->is_allocation_mode==CORRECT){
            return $this->allocation_rate;
        }else{
            return $this->allocation_amount;
        }
    }

    public function getRepurchaseAllocationNumAttribute($value)
    {
        if (!$this->is_repurchase_allocation_mode){
            return '';
        }
        if ($this->is_repurchase_allocation_mode==CORRECT){
            return $this->repurchase_allocation_rate;
        }else{
            return $this->repurchase_allocation_amount;
        }
    }

    public $appends=[
        'allocation_num','repurchase_allocation_num','goods_location'
    ];

    public function getGoodsLocationAttribute($val): string
    {
        $str= sprintf('%s|%s|%s',$this->location_province,$this->location_city,$this->location_area);
        return trim($str,'|');
    }

}
